#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2023, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

source $(dirname $0)/../../etc/vmm/kvm/kvmrc
source $(dirname $0)/../../scripts_common.sh

deploy_id=$1
dest_host=$2
src_host=$3

# migration can't be done with domain snapshots, drop them first
snaps=$(monitor_and_log \
   "virsh --connect $QEMU_PROTOCOL://$src_host/system snapshot-list $deploy_id --name 2>/dev/null" \
   "Failed to get snapshots for $deploy_id")

for snap in $snaps; do
    exec_and_log \
        "virsh --connect $QEMU_PROTOCOL://$src_host/system snapshot-delete $deploy_id --snapshotname $snap --metadata" \
        "Failed to delete snapshot $snap from $deploy_id"
done

# Compact memory on dest host
if [ "x$CLEANUP_MEMORY_ON_START" = "xyes" ]; then
    ssh_exec_and_log "$dest_host" "(sudo -l | grep -q sysctl) && sudo -n sysctl vm.drop_caches=3 vm.compact_memory=1 >/dev/null || true" \
        "Failed compact memory on $dest_host"
fi

# do live migration, but cleanup target host in case of error
virsh --connect $QEMU_PROTOCOL://$src_host/system \
    migrate --live $deploy_id $MIGRATE_OPTIONS $QEMU_PROTOCOL://$dest_host/system

RC=$?

if [ $RC -ne 0 ]; then
    for CLEAN_OP in destroy undefine; do
        virsh --connect $QEMU_PROTOCOL://$dest_host/system "${CLEAN_OP}" $deploy_id >/dev/null 2>&1
    done

    error_message "Could not migrate $deploy_id to $dest_host"
    exit $RC
fi

# Synchronize VM time on background
if [ "$SYNC_TIME" = "yes" ]; then
    (
        for I in $(seq 4 -1 1); do
            if virsh --connect $QEMU_PROTOCOL://$dest_host/system --readonly dominfo $deploy_id; then
                virsh --connect $QEMU_PROTOCOL://$dest_host/system domtime --sync $deploy_id && exit
                [ "$I" -gt 1 ] && sleep 5
            else
                exit
            fi
        done
    ) &>/dev/null &
fi

# Compact memory on src host
if [ "x$CLEANUP_MEMORY_ON_STOP" = "xyes" ]; then
    ssh_exec_and_log "$src_host" "(sudo -l | grep -q sysctl) && (sudo -n sysctl vm.drop_caches=3 vm.compact_memory=1 &>/dev/null &) || true" \
        "Failed compact memory on $src_host"
fi
